package com.admin.action;

import com.admin.service.SoftwareAdminService;
import com.client.bean.SaFeAPK;
import com.data.util.CheckDataUtils;
import com.data.util.FinalStringUtils;
import com.data.util.JsonUtils;
import com.user.bean.Speaking;
import com.user.service.SoftwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 系统管理员的软件管理
 *
 * @author:HingLo
 * @create 2017-09-22 15:17
 **/
@Controller
@RequestMapping("/SoftwareAdmin/admin/")
public class SoftwareAdminAction {

    @Autowired
    private SoftwareAdminService softwareAdminService;
    @Autowired
    private SoftwareService softwareService;


    /**
     * 进入到软件页面
     *
     * @param request
     * @param type
     * @return
     */
    @RequestMapping("software")
    public String inputSoftwareAdminAction(HttpServletRequest request, int type) {
        if (type == FinalStringUtils.SOFTTYPE || type == FinalStringUtils.GAMETYPE) {
            List<?> list = softwareAdminService.findSoftwareAdminService(type);
            request.setAttribute("dataMap", list);
            return "/WEB-INF/admin/software/software";
        } else {
            return "404";
        }
    }


    /***
     * 下线软件申请查看
     * @param request
     * @return
     */
    @GetMapping("downOfflineSoftware")
    public String downOfflineSoftwareAction(HttpServletRequest request) {
        List<?> list = softwareAdminService.downOfflineSoftwareService();
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/software/downoffline";
    }

    /**
     * 下线软件审核
     *
     * @param id
     * @param state
     * @return
     */
    @PostMapping("applyDownOffice")
    @ResponseBody
    public String applyDownOfflineAction(String id, String state) {
        boolean bool = false;
        if (CheckDataUtils.stringUtilsID(id, state)) {
            if (state.equals("-2")) {
                bool = softwareAdminService.applyDownOfflineService(id, -2);
            } else if (state.equals("-3")) {
                bool = softwareAdminService.applyDownOfflineService(id, -3);
            }

        }
        return String.valueOf(bool);
    }

    /**
     * 查看软件的详细信息，图标，分类等
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("softInfo")
    public String lookSoftwareInfoAction(String id, HttpServletRequest request) {
        request.setAttribute("App", softwareService.findSoftwareInfoService(id, 1));
        request.setAttribute("Icon", softwareService.findSoftwareInfoService(id, 2));
        request.setAttribute("Menu", softwareService.findSoftwareInfoService(id, 3));
        return "/WEB-INF/admin/software/softwareInfo";
    }

    /**
     * 查看评论
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("lookSpeakingInfo")
    public String lookSpeakingInfoAction(HttpServletRequest request, @RequestParam("id") String id) {
        List<Speaking> list = (List<Speaking>) softwareAdminService.lookSpeakingInfoService(id);
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/software/speaking";

    }


    /**
     * 删除评论
     *
     * @return
     */
    @PostMapping("delSpeakingInfo")
    @ResponseBody
    public Map<String, Object> delSpeakingInfoAction(HttpServletRequest request, String id) {
        boolean bool = softwareAdminService.delSpeakingInfoService(id);
        String data = bool ? "success" : "fail";
        return JsonUtils.returnMassageUtils(bool, data);
    }

    /**
     * 软件删除，停用，下线等操作
     *
     * @return
     */
    @PostMapping("adminSoftwareState")
    @ResponseBody
    public Map<String, Object> adminSoftwareStateAction(String id, int state) {
        boolean bool = softwareAdminService.adminSoftwareStateService(id, state);
        String data = bool ? "success" : "fail";
        return JsonUtils.returnMassageUtils(bool, data);
    }

    /**
     * 查询软件历史版本
     *
     * @return
     */
    @GetMapping("list")
    public String listSafe(HttpServletRequest request) {
        List<SaFeAPK> list = softwareAdminService.listSafeService();
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/software/safe";
    }

    /**
     * 进入更新软件界面
     *
     * @return
     */
    @GetMapping("addSafe")
    public String inputSafe() {
        return "/WEB-INF/admin/software/addsafe";
    }

    /**
     * 提交安全鱼更新
     *
     * @param content
     * @param apk
     * @param request
     * @return
     */
    @PostMapping("submitSafe")
    @ResponseBody
    public Map<String, Object> submitSafe(@RequestParam("content") String content, @RequestParam("apk") CommonsMultipartFile apk, HttpServletRequest request) {
        boolean bool = softwareAdminService.submitSafeService(content, apk);
        return JsonUtils.returnMassageUtils(bool, bool ? "添加成功" : "添加失败");
    }


}
